package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)//该注解 表示开启事务
    @Override
    public void save(DishDTO dishDTO) {
        // 保存菜品基本数据
        Dish dish = new Dish();
        // 拷贝属性
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);
        // 保存菜品口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long id = dish.getId();
        log.info("菜品id:{}",id);
        log.info("菜品口味数据:{}",flavors);
        dishFlavorMapper.saveBatch(flavors, id);
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<DishVO> list = dishMapper.page(dishPageQueryDTO);
        Page page = (Page) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 菜品起售、停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Long empId = BaseContext.getCurrentId();
        dishMapper.startOrStop(status,id,empId);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        //先查询菜品基本数据
        DishVO dishVO = dishMapper.getById(id);
        // 查询菜品口味数据
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        // 封装到DishVO对象中
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void update(DishDTO dishDTO) {
        // 修改菜品基本数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        // 修改菜品口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long id = dish.getId();
        dishFlavorMapper.deleteByDishId(id);
        dishFlavorMapper.saveBatch(flavors, id);
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //TODO 其他业务判断
        for (Long id : ids) {
            dishMapper.delete(id);
            dishFlavorMapper.deleteByDishId(id);
        }
    }
}
